﻿using System;
namespace HW_2
{
    class b_1
    {
        static void Main()
        {
            //设置对齐参数
            Console.WriteLine("{0,-10} {1,-10} {2,-30} {3,-30}", "Type", "Bytes", "Min Value", "Max Value");
            Console.WriteLine(new string('-', 80));

            PrintTypeInfo<sbyte>("sbyte");
            PrintTypeInfo<byte>("byte");
            PrintTypeInfo<short>("short");
            PrintTypeInfo<ushort>("ushort");
            PrintTypeInfo<int>("int");
            PrintTypeInfo<uint>("uint");
            PrintTypeInfo<long>("long");
            PrintTypeInfo<ulong>("ulong");
            PrintTypeInfo<float>("float");
            PrintTypeInfo<double>("double");
            PrintTypeInfo<decimal>("decimal");
        }

        //写模版函数,打印出来更方便;
        //where是进行泛型约束,表示类型参数 T 必须是一个值类型（struct），并且实现了 IComparable、IFormattable 和 IConvertible 接口
        static void PrintTypeInfo<T>(string typeName) where T : struct, IComparable, IFormattable, IConvertible
        {
            Type type = typeof(T);
            Console.WriteLine("{0,-10} {1,-10} {2,-30} {3,-30}",
                typeName,
                System.Runtime.InteropServices.Marshal.SizeOf(type),
                Convert.ChangeType(type.GetField("MinValue").GetValue(null), type),
                Convert.ChangeType(type.GetField("MaxValue").GetValue(null), type)
            );
        }
    }
}

